package org.zuel.controller;

import org.zuel.entity.User;
import org.zuel.service.IUserRoleService;
import org.zuel.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.zuel.service.impl.UserRoleServiceImpl;
import org.zuel.web.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 *  控制器
 *
 * @author ikun
 */
@Api(tags = "管理")
@RestController
@RequestMapping("")
public class UserController{
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    public UserController(IUserService userService){
        this.userService = userService;
    }

    @ApiOperation("查询列表")
    @GetMapping("/user/list")
    public PageResult<User> list(HttpServletRequest request) {
        PageParam<User> pageParam = new PageParam<>(request);
        return userService.list(pageParam);
    }

    @ApiOperation("查询单个")
    @GetMapping("/user/read/{id}")
    public JsonResult read(@PathVariable("id") Integer id) {
        return userService.read(id);
    }

    @ApiOperation("添加")
    @PostMapping("/user/regist")
    public JsonResult add(@RequestBody User model) {
        JsonResult a=userService.add(model);
        userRoleService.addUser(model);
        return a;
    }

    @ApiOperation("修改")
    @PostMapping("/user/update")
    public JsonResult update(@RequestBody User model) {
    return userService.update(model);
    }

    @ApiOperation("删除")
    @GetMapping("/user/delete/{ids}")
    public JsonResult delete(@PathVariable("ids") Integer[] ids) {
        return userService.delete(ids);
    }

}